﻿using Microsoft.AspNetCore.Mvc;
using ModelDto;
using WorkFlow.WorkFlow;
using WorkFlow;
using WorkflowCore.Interface;

namespace WebApplication1.Controllers
{
    [ApiController]
    [Route("api/[controller]/[action]")]
    public class WorkFlowCeshiController : ControllerBase
    {
        private readonly IWorkflowHost _workflowHost;
        public WorkFlowCeshiController(IWorkflowHost workflowHost)
        {
            _workflowHost= workflowHost;
        }
        [HttpGet(Name = "get")]
        public ContentResult NoInputGet()
        {
            if (!_workflowHost.Registry.IsRegistered("HelloWorld", 1))
            {
                _workflowHost.RegisterWorkflow<NoInputWorkFlow>();
            }
            _workflowHost.Start();
            _workflowHost.StartWorkflow("HelloWorld");
            //host.Stop();
            return Content("ok");
        }
        //启动工作流
        [HttpGet]
        public ContentResult Get()
        {
            //执行工作流传入参数
            MyDataClass myDataClass = new MyDataClass();
            myDataClass.Value1 = 100;
            myDataClass.Value2 = 200;
            myDataClass.secondDataClass.First = "First";
            myDataClass.secondDataClass.Second = "Second";
            //判断流有没有启动，已启动再执行工作流会抛异常
            if (!_workflowHost.Registry.IsRegistered("Zqh", 1))
            {
                _workflowHost.RegisterWorkflow<MyWorkflow, MyDataClass>();
                //不传入myDataClass则每次执行都是新的数据对象

                _workflowHost.Start();
                _workflowHost.StartWorkflow("Zqh", myDataClass);
            }
            else
            {
                return Content("流已开启");
            }
            

            //根据workflowId结束工作流
            //_workflowHost.TerminateWorkflow("Zqh");
            return Content("OK");

        }
        /// <summary>
        /// 重启工作流
        /// </summary>
        /// <returns></returns>
        [HttpPost(Name = "event")]
        public ContentResult PublishEvent()
        {
            _workflowHost.PublishEvent("MyEvent", "EventKey", 200);
            return Content("ok");
        }
    }
}
